package com.blog.controller;

import com.blog.entity.Users;
import com.blog.vos.CollectionVOS;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
//@RequestMapping("/orders")
public class OrdersController {

    @GetMapping
    public String demo1(){
        System.out.println("调用了Orders的微服务");
        return "调用了Orders的微服务";
    }

    //接收零散参数
    @GetMapping("/demo2/{uname}/{age}")
    public String demo2(@PathVariable("uname") String uname, @PathVariable("age")  Integer age){
        System.out.println(uname);
        System.out.println(age);
        return "demo2";
    }

    //接收对象
    @PostMapping("/demo3")
    public String demo3(@RequestBody Users users){
        System.out.println(users);
        return "demo3";
    }

    //接收数组
    @GetMapping("/demo4")
    public String demo4(@RequestParam("uNames") String[] uNames){
        System.out.println( uNames);
        return "demo4";
    }

    //接收集合
    @GetMapping("/demo5")
    public String demo5(@RequestBody CollectionVOS collectionVOS){
        List<String> names = collectionVOS.getNames();
        System.out.println(names);
        return "demo5";
    }

    /**
     * 处理响应
     */
    //接收id,返回对象
    @GetMapping("/demo6/{id}")
    public Users demo6(@PathVariable("id") Integer id){
        System.out.println("id="+id);
        return new Users("狗春",1,new Date());
    }

    //接收id,返回Map
    @GetMapping("/demo7/{id}")
    Map<String,Object> demo7(@PathVariable("id") Integer id) throws InterruptedException {
        List <Users> usersList=new ArrayList<>();
        usersList.add(new Users("狗春",13,new Date()));
        usersList.add(new Users("狗财",15,new Date()));
        usersList.add(new Users("狗亮",12,new Date()));
        Map<String,Object> info=new HashMap<>();
        info.put("total","250");
        info.put("usersList",usersList);
        return info;
    }

    //Hystrix熔断
    @GetMapping("/demo8")
    public String demo8(@RequestParam("id") Integer id){
        if (id<0){
            throw new RuntimeException("调用失败");
        }
        return "调用demo8成功";
    }


}
